<?php

class JamController {
	
	private $_name;
	private $_action;

	public function  __construct( $name ) {
		$this->_name = $name;
	}

	public function execute( $actionName ){
		$this->_action = $actionName = JString::strtolower( $actionName ) . 'Action';
		$className = JAM_COMPONENT_PREFIX . 'Controller' . JString::ucfirst( JString::strtolower( $this->_name ) );
		if( method_exists( $className, $this->_action ) )
			$this->$actionName();
		else
			JError::raiseError( 500, 'Action ' . $actionName . ' doesn\'t exist!' );
	}

	protected function & getModel( $modelName ){
		JamLoader::loadModel( $modelName );
		$modelClassName = JAM_COMPONENT_PREFIX . 'Model' . JString::ucfirst( JString::strtolower( $modelName ) );
		$model = new $modelClassName();
		return $model;
	}

	protected function & getView(){
		JamLoader::loadView( $this->_name ); // the view file name corresponds to the controller file name, the model may be different
		$viewClassName = JAM_COMPONENT_PREFIX . 'View' . JString::ucfirst( JString::strtolower( $this->_name ) );
		$view = new $viewClassName();
		return $view;
	}

	protected function redirect($url, $message = null){
		global $mainframe;

		$mainframe->redirect($url, $message);
	}

	/* The default action should run by itself */
	public function indexAction(){
		$view = $this->getView();
		$view->display();
	}
}

?>